package com.youlian.cloud.system.config;

import com.youlian.cloud.core.intercept.CheckAnnotationProcess;
import com.youlian.cloud.core.util.RestTemplateUtils;
import com.youlian.cloud.spring.config.DataSourceConfigBean;
import com.youlian.cloud.spring.config.JdbcBeanConfig;
import com.youlian.cloud.spring.config.TransactionConfigBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.*;
import org.springframework.core.env.Environment;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.client.RestTemplate;

/**
 * @author liuhx
 * @create 2019/08/01 17:28
 * @email liuhx@elab-plus.com
 **/
@Configuration
@ComponentScan(basePackages = {"com.youlian.cloud.system.service"}, excludeFilters = {
        @ComponentScan.Filter(type = FilterType.ANNOTATION, value = {Controller.class, ControllerAdvice.class})
})
@Import({DataSourceConfigBean.class,JdbcBeanConfig.class, TransactionConfigBean.class})
public class BeanConfig {

    @Bean
    public ClientHttpRequestFactory getClientHttpRequestFactory(@Autowired Environment environment) {
        SimpleClientHttpRequestFactory simpleClientHttpRequestFactory = new SimpleClientHttpRequestFactory();
        simpleClientHttpRequestFactory.setConnectTimeout(Integer.valueOf(environment.getProperty("httpClient.connect.timeOut", "120000")));
        simpleClientHttpRequestFactory.setReadTimeout(Integer.valueOf(environment.getProperty("httpClient.read.timeOut", "120000")));
        return simpleClientHttpRequestFactory;
    }

    @Bean
    public RestTemplate getRestTemplate(@Autowired Environment environment) {
        RestTemplate restTemplate = new RestTemplate(getClientHttpRequestFactory(environment));
        return restTemplate;
    }

    @Bean
    @Profile("dev")
    public CheckAnnotationProcess checkAnnotationProcess(@Autowired Environment environment) {
        CheckAnnotationProcess checkAnnotationProcess = new CheckAnnotationProcess(environment);
        return checkAnnotationProcess;
    }

    @Bean
    public RestTemplateUtils restTemplateUtils(@Autowired RestTemplate restTemplate) {
        RestTemplateUtils restTemplateUtils = new RestTemplateUtils();
        restTemplateUtils.setRestTemplate(restTemplate);
        return restTemplateUtils;
    }
}
